package com.tmall.testgen.server.socket.handler;

import com.tmall.testgen.common.bo.interact.apply.ApplyCommond;
import com.tmall.testgen.common.bo.interact.apply.ApplyConnection;
import com.tmall.testgen.common.bo.interact.back.TransferCarFromClient;
import com.tmall.testgen.common.bo.interact.back.TransferCarFromServer;
import com.tmall.testgen.common.bo.interact.back.syn.ConnectionSuccResult;
import com.tmall.testgen.common.bo.interact.back.syn.SendCommonResult;
import com.tmall.testgen.common.socket.ReceiveObjectHandler;
import com.tmall.testgen.common.util.ConfigExtractor;
import com.tmall.testgen.server.agent.Transformer4AOP;
import com.tmall.testgen.server.infocollect.DataCenter;
import com.tmall.testgen.server.infocollect.UserData;
import com.tmall.testgen.server.socket.CommondEnum;

/**
 * @author haojia
 * @date 2014-07-04
 */
public class ServerReceiveObjectHandler  implements  ReceiveObjectHandler  {

	@Override
	public void handleReceiveObj(Object receive) {
		
		if(!(receive instanceof TransferCarFromClient )){
			System.out.println(receive.getClass().getName());
			return;
		}
		
		TransferCarFromClient car=(TransferCarFromClient) receive;
		
		switch(car.getType()){
			case TransferCarFromClient.TYPE_APPLY_CONNECTION:
				handleReceiveApplyConnection((ApplyConnection) car.getData());
				break;
			case TransferCarFromClient.TYPE_APPLY_COMMOND:
				handleReceiveApplyCommond((ApplyCommond) car.getData());
				break;
		}
		
		
	}
	
	/**
	 * 接收到申请连接的处理
	 */
	private void handleReceiveApplyConnection(ApplyConnection apply){
		DataCenter.addApplyConnection(apply);
		try {
			Transformer4AOP.continueRetransform();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("拦截结束：");
		ConnectionSuccResult result=new ConnectionSuccResult();
		result.setDbInfos(ConfigExtractor.getDB());
		//System.out.println("db结束：");
		UserData userData=DataCenter.getUserDatas().get(apply.getUserId());
		//System.out.println("获取userId："+apply.getUserId());
		if(userData!=null){
			DataCenter.refresh();
			userData.setStart(true);
			userData.writeObject(new TransferCarFromServer(TransferCarFromServer.TYPE_CONNECTION_SUCC_RESULT,result));
		}
		
	}
	
	/**
	 * 接收到请求命令的处理
	 * @param commond
	 */
	private void handleReceiveApplyCommond(ApplyCommond commond){
		SendCommonResult result=new SendCommonResult();
		result.setCommond(commond.getCommond());
		
		String userId=commond.getUserId();
		System.out.println("接受的userId"+userId);
		int code=commond.getCommond().getCommondCode();
		UserData userData=DataCenter.getUserDatas().get(userId);
		if(userData==null){
			//result.setResult(false);
			//result.setMsg("user is not exist");
			//userData.writeObject(new TransferCarFromServer(TransferCarFromServer.TYPE_SEND_COMMOND_RESULT,result));
			return;
		}
		
		if(CommondEnum.BeginIntercept.getCommondCode()==code){
			System.out.println("开始拦截");
			userData.setStart(true);
		}else if(CommondEnum.Pause.getCommondCode()==code){
			System.out.println("暂停拦截");
			userData.setStart(false);
		}else if(CommondEnum.Stop.getCommondCode()==code){
			System.out.println("终止");
			DataCenter.getUserDatas().remove(userId);
		}else if(CommondEnum.Clear.getCommondCode()==code){
			System.out.println("清空");
			userData.setInfoes(null);
		}
		userData.writeObject(new TransferCarFromServer(TransferCarFromServer.TYPE_SEND_COMMOND_RESULT,result));
	}

}
